window.action = window.action || {};
action.Action = function () {
    var s = this;
    annie.Sprite.call(s);
    /*_a2x_need_start*/s.botton_mc=null;s.center_mc=null;s.topBtns_mc=null;/*_a2x_need_end*/
    annie.initRes(s, "action", "Action");
    s.topBtns_mc.back_btn.initButton();
    s.topBtns_mc.complete_btn.initButton();
    s.addEventListener(annie.Event.ADD_TO_STAGE, function (e) {
        s.botton_mc.y = s.stage.viewRect.height + s.stage.viewRect.y;
        s.topBtns_mc.y = s.stage.viewRect.y + 20;
    });
    //当前操作元素
    var currOpObj = null;
    var itemList = [];
    var bgList = [];
    var itemCheckList=action.itemCheckList;
    var currOpObj=new a0.Icon0();
    currOpObj.scaleY=currOpObj.scaleX=0.4;
    s.center_mc.addChild(currOpObj);
    itemList[0]=currOpObj;
    annie.globalDispatcher.addEventListener("onCheckItem", function (e) {
        isBegin = false;
        var iconItem = e.data.obj;
        if (iconItem.type < 6) {
            //元素
            if(action.checkId!=iconItem.id){
                var child=null;
                for(var i=0;i<iconItem.parent.children.length;i++){
                    child=iconItem.parent.children[i];
                    if(child.id==action.checkId){
                        child.yy_mc.visible=false;
                        child.red_mc.gotoAndStop(2);
                        annie.Tween.to(child.obj,0.2,{scaleX:0.78,scaleY:0.78,ease:annie.Tween.backInOut});
                        break;
                    }
                }
                action.checkId=iconItem.id;
                iconItem.yy_mc.visible=true;
                annie.Tween.to(iconItem.obj,0.2,{scaleX:1,scaleY:1,ease:annie.Tween.backInOut});
                if(iconItem.id>0) {
                    iconItem.red_mc.gotoAndStop(3);
                }else{
                    iconItem.red_mc.gotoAndStop(1);
                }
                if (!itemCheckList[iconItem.id]) {
                    if (!itemList[iconItem.id]) {
                        itemList[iconItem.id] = new a0["Icon" + iconItem.id]();
                    }
                    itemCheckList[iconItem.id] = true;
                }
                currOpObj = itemList[iconItem.id];
                runType = iconItem.runType;
                var index=s.center_mc.children.length-1;
                if(iconItem.id!=0) {
                    if (currOpObj.parent)
                        index -= 1;
                }
                s.center_mc.addChildAt(currOpObj,index);
            }else{
                if(action.checkId>0) {
                    iconItem.yy_mc.visible = false;
                    annie.Tween.to(iconItem.obj, 0.2, {scaleX: 0.78, scaleY: 0.78, ease: annie.Tween.backInOut});
                    s.center_mc.removeChild(currOpObj);
                    action.checkId = -1;
                    itemCheckList[iconItem.id]=false;
                    iconItem.red_mc.gotoAndStop(1);
                    currOpObj = null;
                }
            }
        } else {
            //背景
            var yyMc=iconItem.parent.yy_mc;
            if (!bgList[iconItem.id]){
                bgList[iconItem.id] = new a0["Bg" + iconItem.id]();
            }
            if (!bgList[iconItem.id].parent) {
                s.center_mc.addBg_mc.removeAllChildren();
                s.center_mc.addBg_mc.addChild(bgList[iconItem.id]);
                //改变按钮状态
                var bgObj=null;
                for(var i=0;i<3;i++){
                    bgObj=iconItem.parent["bg"+(i+1)];
                    if(bgObj.id!=iconItem.id){
                        bgObj.scaleY=bgObj.scaleX=0.78;
                    }else{
                        bgObj.scaleY=bgObj.scaleX=1;
                        yyMc.x=bgObj.x;
                        yyMc.visible=true;
                    }
                }
            }else{
                //删除背景
                yyMc.visible=false;
                iconItem.scaleY=iconItem.scaleX=0.78;
                s.center_mc.addBg_mc.removeAllChildren();
                s.center_mc.defaultBg_mc.visible = true;
            }

        }
        e.data.iconItem = null;
    });
    //是否开始
    var isBegin = false;
    //运动类型
    var runType = 0;
    var beginDis = 200;
    var lastDis = 200;
    var beginAng = 0;
    var beginScale = 1;
    var mouseDownAndOver = function (e){
        if(!currOpObj)return;
        isBegin = true;
        beginDis = annie.Point.distance(e.localX, e.localY, 0, 0);
        beginAng = Math.atan2(e.localY, e.localX);
        beginScale = beginDis / 350;
    };
    var mosueUpAndOut=function(e){
        if(!currOpObj)return;
        if(runType==2||runType==3) {
            var rotationDis = currOpObj.rotation % 45;
            var newRotation = currOpObj.rotation - rotationDis;
            if (rotationDis > 22.5) {
                newRotation += 45;
            }
            annie.Tween.to(currOpObj, 0.2, {rotation: newRotation});
        }
    }
    s.center_mc.addEventListener(annie.MouseEvent.MOUSE_DOWN, mouseDownAndOver);
    s.center_mc.addEventListener(annie.MouseEvent.MOUSE_OVER, mouseDownAndOver);
    s.center_mc.addEventListener(annie.MouseEvent.MOUSE_UP, mosueUpAndOut);
    s.center_mc.addEventListener(annie.MouseEvent.MOUSE_OUT, mosueUpAndOut);
    s.center_mc.addEventListener(annie.MouseEvent.MOUSE_MOVE, function (e) {
        if(!currOpObj)return;
        beginDis = annie.Point.distance(e.localX, e.localY, 0, 0);
        beginScale = beginDis / 350;
        var newAng = Math.atan2(e.localY, e.localX);
        var dis = newAng - beginAng;
        beginAng = newAng;
        if (runType == 1) {
            for (var i = 0; i < 16; i++) {
                var a = currOpObj.getChildAt(i);
                a.angle += dis
            }
        } else {
            currOpObj.rotation += dis * 180 / Math.PI;
        }
    });
    s.center_mc.addEventListener(annie.Event.ENTER_FRAME, function (e) {
        if (isBegin&&currOpObj) {
            if (runType == 1) {
                if (lastDis !=beginDis) {
                    lastDis += 0.2 * (beginDis - lastDis);
                    for (var i = 0; i < 16; i++) {
                        var a = currOpObj.getChildAt(i);
                        a.x = Math.cos(a.angle) * lastDis;
                        a.y = Math.sin(a.angle) * lastDis;
                    }
                } else {
                    isBegin = false;
                }
            } else{
                currOpObj.scaleY = currOpObj.scaleX += 0.2 * (beginScale - currOpObj.scaleX);
            }
        }
    })

};
A2xExtend(action.Action, annie.Sprite);
action.checkId=0;
action.iconList=[];
action.itemCheckList = [];

